package blog.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import blog.entity.Category;
import blog.service.CategoryService;
/**
 * 文章种类控制层
 * @author 江佳洋
 *
 */
@Controller
@RequestMapping("category")
public class CategoryController {
	@Autowired
	private CategoryService categoryService;
	@RequestMapping("/list")
	public String findAllCategory(ModelMap map) {
		List<Category> findAllCategory = categoryService.findAllCategory();
		//放所有大分类  防止分类顺序乱的时候 前端页面按顺序排列出问题
		List<Category> bigCategory = new ArrayList<>();
		for(Category big :findAllCategory) {
			if(big.getCategoryPid()==0) {
				bigCategory.add(big);
			}
		}
		map.put("bigCategory", bigCategory);
		map.put("category", findAllCategory);
		return "article/article_cartgory_list";
	}
	/**
	 * 判断用户名是否重复
	 * @param map
	 * @param categoryName
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/categoryName")
	@ResponseBody
	public void categoryName(ModelMap map,String categoryName,HttpServletResponse response) throws IOException {
		String getcategoryName = categoryService.getcategoryName(categoryName);
		if(categoryName.equals(getcategoryName)) {
			response.setContentType("text/html;charset=utf8");
			response.getWriter().print("1");
		}
	}
	/**
	 * 添加分类
	 * @param map
	 * @param category
	 * @return
	 */
	@RequestMapping("/insertSubmit")
	public String insertSubmit(ModelMap map,Category category) {
		categoryService.addCategory(category);
		map.put("cate", category);
		List<Category> findAllCategory = categoryService.findAllCategory();
		//放所有大分类  防止分类顺序乱的时候 前端页面按顺序排列出问题
		List<Category> bigCategory = new ArrayList<>();
		for(Category big :findAllCategory) {
			if(big.getCategoryPid()==0) {
				bigCategory.add(big);
			}
		}
		map.put("bigCategory", bigCategory);
		map.put("category", findAllCategory);
		return "article/article_cartgory_list";
	}
	/**
	 * 根据id删除分类
	 * @param map
	 * @param categoryId
	 * @return
	 */
	@RequestMapping("/delete/{categoryId}")
	public String deleteCategory(ModelMap map,@PathVariable("categoryId") Integer categoryId) {
		categoryService.deleteCategory(categoryId);
		List<Category> findAllCategory = categoryService.findAllCategory();
		//放所有大分类  防止分类顺序乱的时候 前端页面按顺序排列出问题
		List<Category> bigCategory = new ArrayList<>();
		for(Category big :findAllCategory) {
			if(big.getCategoryPid()==0) {
				bigCategory.add(big);
			}
		}
		map.put("bigCategory", bigCategory);
		map.put("category", findAllCategory);
		return "article/article_cartgory_list";
	}
	/**
	 * 根据id查询出要修改的分类
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping("/edit/{categoryId}")
	public String editCategory(@PathVariable("categoryId") Integer id,ModelMap map) {
		Category selectCategoryById = categoryService.selectCategoryById(id);
		map.put("cate", selectCategoryById);
		map.put("flag", "edit");
		List<Category> findAllCategory = categoryService.findAllCategory();
		//放所有大分类  防止分类顺序乱的时候 前端页面按顺序排列出问题
		List<Category> bigCategory = new ArrayList<>();
		for(Category big :findAllCategory) {
			if(big.getCategoryPid()==0) {
				bigCategory.add(big);
			}
		}
		map.put("bigCategory", bigCategory);
		map.put("category", findAllCategory);
		return "article/article_cartgory_list";
	}
	
	/**
	 * 添加分类
	 * @param map
	 * @param category
	 * @return
	 */
	@RequestMapping("/updateSubmit")
	public String updateSubmit(ModelMap map,Category category) {
		categoryService.updateCategoru(category);
		map.put("cate", category);
		List<Category> findAllCategory = categoryService.findAllCategory();
		//放所有大分类  防止分类顺序乱的时候 前端页面按顺序排列出问题
		List<Category> bigCategory = new ArrayList<>();
		for(Category big :findAllCategory) {
			if(big.getCategoryPid()==0) {
				bigCategory.add(big);
			}
		}
		map.put("bigCategory", bigCategory);
		map.put("category", findAllCategory);
		return "article/article_cartgory_list";
	}
}
